var ModuleUtil = {
  getAttendanceDelayTime: function (clock, status, leaveList) {
    var userCheckTime = clock.userCheckTime;
    var baseCheckTime = clock.baseCheckTime;
    var startMoment, endMoment;
    if (status === 'late') {
      startMoment = moment(baseCheckTime);
      endMoment = moment(userCheckTime);
    } else {
      startMoment = moment(userCheckTime);
      endMoment = moment(baseCheckTime);
    }

    var rangeTime = moment(userCheckTime).diff(baseCheckTime);
    rangeTime = Math.abs(rangeTime);

    leaveList = _.filter(leaveList, function (leave) {
      var startTime = leave.startTime;
      var endTime = leave.endTime;
      return moment(startTime).isBetween(startMoment, endMoment, 'minute', '[]') ||
        moment(endTime).isBetween(startMoment, endMoment, 'minute', '[]') ||
        startMoment.isBetween(startTime, endTime, 'minute', '[]') ||
        endMoment.isBetween(startTime, endTime, 'minute', '[]');
    });

    if (leaveList.length === 0) {
      return rangeTime;
    }

    var totalLeaveTime = 0;
    for (var i = 0; i < leaveList.length; i++) {
      var leave = leaveList[i];
      var startTime = leave.startTime;
      var endTime = leave.endTime;
      startTime = moment.max(moment(startTime), startMoment);
      endTime = moment.min(moment(endTime), endMoment);
      var leaveTime = endTime.diff(startTime);
      totalLeaveTime += leaveTime;
    }

    rangeTime -= totalLeaveTime;
    if (rangeTime === 0) {
      var leave;
      if (status === 'late') {
        leave = leaveList[0];
      } else {
        leave = leaveList[leaveList.length - 1];
      }

      if (leave !== undefined) {
        leave = _.cloneDeep(leave);
        leave.startTime = moment(leave.startTime).valueOf();
        leave.endTime = moment(leave.endTime).valueOf();
        clock.leaveData = leave;
      }
    }
    return rangeTime;
  }
};